<script setup lang="ts">
import { getBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import CustomNavbar from './components/CustomNavbar.vue'
import { ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import CategoryPanel from './components/CategoryPanel.vue'
import HotPanel from './components/HotPanel.vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import PageSkeleton from './components/PageSkeleton.vue'
import { useGuess } from '@/composables/useGuess'

onLoad(async () => {
  await Promise.all([getBanner(), getCategory(), getHot()])
  isLoading.value = false
})

const bannerList = ref<BannerItem[]>([])
const getBanner = async () => {
  const res = await getBannerAPI()
  bannerList.value = res.result
}

const categoryList = ref<CategoryItem[]>([])
const getCategory = async () => {
  const res = await getHomeCategoryAPI()
  categoryList.value = res.result
}

const hotList = ref<HotItem[]>([])
const getHot = async () => {
  const res = await getHomeHotAPI()
  hotList.value = res.result
}

const { guessRef, onScrolltolower } = useGuess()

const isRefresh = ref(false)
const refresh = async () => {
  isRefresh.value = true
  guessRef.value?.reset()
  await Promise.all([getBanner(), getCategory(), getHot()])
  guessRef.value?.getLike()
  isRefresh.value = false
}

const isLoading = ref(true)
</script>

<template>
  <!-- 自定义导航栏 -->
  <CustomNavbar />
  <scroll-view
    refresher-enabled
    @refresherrefresh="refresh()"
    :refresher-triggered="isRefresh"
    @scrolltolower="onScrolltolower()"
    class="scroll"
    scroll-y
  >
    <PageSkeleton v-if="isLoading" />
    <template v-else>
      <!-- 轮播图 -->
      <XtxSwiper :list="bannerList" />
      <!-- 分类面板 -->
      <CategoryPanel :categoryList="categoryList" />
      <!-- 热门推荐 -->
      <HotPanel :hotList="hotList" />
      <!-- 猜你喜欢组件 -->
      <XtxGuess ref="guessRef" />
    </template>
  </scroll-view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  display: flex;
  flex-direction: column;

  .scroll {
    flex: 1;
  }
}
</style>
